package samsung.sds.system.intercepter;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public abstract class BaseIntercepter extends HandlerInterceptorAdapter {
	private static Logger log = Logger
			.getLogger(BaseIntercepter.class);
	public final boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		
		log.debug("");
		log.debug("");
		log.debug("########################################");
		log.debug("custom intercepter:"+this.getClass().getName());
		log.debug("request uri:"+request.getRequestURI());
		
		//拥有此拦截器的Passport时不进行校验
		if(this.isHavePassport(request, response, object)){
			log.debug("passport validated.");
			log.debug("########################################");
			return true;
		}
		
		//排除在外的url不进行拦截
		String urls[] = null ;		
		Map<String, Object> map = getParam();		
		if (null != map.get("excludeUrls")) {
			urls = map.get("excludeUrls").toString().split(",");
		}
		String requestUrl = request.getRequestURI();
		String redirectUrl = "" ;		
		if (null != map.get("redirectUrl")) {
			redirectUrl = map.get("redirectUrl").toString();
		}

		for(String url : urls){			
    		if (requestUrl.equals(request.getContextPath() + url.trim())) {   
    			log.debug(" uri is in exclude list!");
    			log.debug("########################################");
    			return true;
			}
    	}
		
		log.debug("start interceptor logic check.");		
		//调用自定义规则，判断是否进行拦截
		boolean result = this.checkLogic(request, response, object);		
		
		if(result){//规则命中时进行跳转
			response.sendRedirect(request.getContextPath() + redirectUrl);
			log.debug("rule matched");
			log.debug("########################################");
			return false;
		}else{
			log.debug("rule passed");
			log.debug("########################################");
			return true;	
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}
	/**
	 * 子类需要实现的判断 逻辑： 若返回True，即命中规则，进行拦截，返回False不进行拦截
	 * @param request
	 * @param response
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public abstract boolean checkLogic(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception;
	
	/**
	 * 判断当前Session是否拥有此拦截器的通行证
	 * 有通行证即不进行任何逻辑校验，直接放行，提高处理效率
	 * 为效率考虑，此方法为抽象方法，如子类不需要，直接返回False即可。
	 * @param request
	 * @param response
	 * @param object
	 * @return
	 * @throws Exception
	 */
	public abstract boolean isHavePassport(HttpServletRequest request, HttpServletResponse response, Object object);
	
	/**
	 * 获取跳转URL和排除的URL
	 * @return
	 */
	public abstract Map<String, Object> getParam();
		
}
